/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doancnpm1;

import Login.AddNauAn;
import Login.Login;
import connect.DBUtil;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author soul-lyno
 */
public class MonAn extends javax.swing.JFrame {
    DefaultTableModel model;
    private String maMonAn = null;
    /**
     * Creates new form NewJFrame
     */
    public MonAn() {
        initComponents();
        
         LoadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel(){
            ImageIcon icon = new ImageIcon("src/doancnpm1/images/.jpg");
            public void paintComponent(Graphics g){
                Dimension d = getSize();
                g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
                setOpaque(false);
                super.paintComponent(g);
            }
        };
        jPanel1 = new javax.swing.JPanel();
        txtMaMonAn = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tab_monan = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txt_tenmonan = new javax.swing.JTextField();
        cmd_xoa = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_quytrinh = new javax.swing.JTextArea();
        cmd_sua = new javax.swing.JButton();
        cmd_them = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ta_nguyenlieu = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        ta_thietbi = new javax.swing.JTextArea();
        cmd_dong = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Món Ăn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 14), new java.awt.Color(102, 51, 255))); // NOI18N

        txtMaMonAn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMaMonAnActionPerformed(evt);
            }
        });

        tab_monan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Món Ăn", "Tên Món Ăn", "Nguyên Liệu", "Thiết Bị", "Quy Trình"
            }
        ));
        tab_monan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tab_monanMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tab_monanMouseEntered(evt);
            }
        });
        jScrollPane2.setViewportView(tab_monan);

        jLabel3.setText("Quy Trình");

        cmd_xoa.setText("Xóa");
        cmd_xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_xoaActionPerformed(evt);
            }
        });

        ta_quytrinh.setColumns(20);
        ta_quytrinh.setRows(5);
        jScrollPane1.setViewportView(ta_quytrinh);

        cmd_sua.setText("Sửa");
        cmd_sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_suaActionPerformed(evt);
            }
        });

        cmd_them.setText("Thêm");
        cmd_them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_themActionPerformed(evt);
            }
        });

        jLabel2.setText("Tên Món An");

        jLabel1.setText("Mã Món Ăn");

        jLabel4.setText("Nguyên Liệu");

        ta_nguyenlieu.setColumns(20);
        ta_nguyenlieu.setRows(5);
        jScrollPane3.setViewportView(ta_nguyenlieu);

        jLabel5.setText("Thiết Bị");

        ta_thietbi.setColumns(20);
        ta_thietbi.setRows(5);
        jScrollPane4.setViewportView(ta_thietbi);

        cmd_dong.setText("Đóng");
        cmd_dong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_dongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txt_tenmonan)
                                .addGap(385, 385, 385))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtMaMonAn, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jScrollPane3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(61, 61, 61))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cmd_them, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(cmd_xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cmd_sua, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cmd_dong, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(156, 156, 156))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaMonAn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_tenmonan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel5)
                        .addGap(95, 95, 95)
                        .addComponent(jLabel3)))
                .addGap(19, 19, 19)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd_sua)
                    .addComponent(cmd_dong)
                    .addComponent(cmd_xoa)
                    .addComponent(cmd_them)))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(162, 162, 162))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 633, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmd_themActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_themActionPerformed
        // TODO add your handling code here: 
        if (txtMaMonAn.getText().equals("")) {
            txtMaMonAn.setBackground(Color.yellow);
            JOptionPane.showMessageDialog(null, "Truong ma mon an khong the de trong, moi ban nhap vao");
        } else if (txt_tenmonan.getText().equals("")) {
            txt_tenmonan.setBackground(Color.yellow);
            JOptionPane.showMessageDialog(null, "Ten mon an trong, moi ban nhap vao");
        } else if (ta_nguyenlieu.getText().equals("")) {
            ta_nguyenlieu.setBackground(Color.yellow);
            JOptionPane.showMessageDialog(null, "Chua co nguyen lieu, moi ban nhap vao");
        } else if (ta_thietbi.getText().equals("")) {
            ta_thietbi.setBackground(Color.yellow);
             JOptionPane.showMessageDialog(null, "Moi ban nhap vao thiet bi che bien");
        } else if (ta_quytrinh.getText().equals("")) {
            ta_quytrinh.setBackground(Color.yellow);
            JOptionPane.showMessageDialog(null, "Moi ban nhap vao quy trinh che bien");
        } else{
            DBUtil objDBUtil = new DBUtil();
            Connection conn = objDBUtil.connect();
            try{
                AddNauAn.AddNauAn(txtMaMonAn.getText(), txt_tenmonan.getText(), ta_nguyenlieu.getText(), ta_thietbi.getText(), ta_quytrinh.getText());
                JOptionPane.showMessageDialog(this, "them thanh cong", "thong bao", JOptionPane.CLOSED_OPTION);
            
                while (model.getRowCount()>0) {                
                    model.removeRow(0);
                }LoadData();
                Clear();
                
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_cmd_themActionPerformed

    private void cmd_suaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_suaActionPerformed
        // chỉnh sửa dữ liêu trong data
        // TODO add your handling code here:
        String maMonAnSua = txtMaMonAn.getText();
        String tenMonAnSua = txt_tenmonan.getText();
        String taNguyenLieu = ta_nguyenlieu.getText();
        String taThietBi = ta_thietbi.getText();
        String taQuyTrinh = ta_quytrinh.getText();
        try {
            AddNauAn.EditNauAn(maMonAnSua, tenMonAnSua, taNguyenLieu, taThietBi, taQuyTrinh);
            JOptionPane.showMessageDialog(null, "update thanh cong");
            while (model.getRowCount()>0) {                
                model.removeRow(0);
            }LoadData();
              Clear();
            
        } catch (SQLException ex) {
            Logger.getLogger(MonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cmd_suaActionPerformed

    private void cmd_dongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_dongActionPerformed
        // TODO add your handling code here:
        // đóng kết nối và giao dien
        this.dispose();
    }//GEN-LAST:event_cmd_dongActionPerformed

    private void cmd_xoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_xoaActionPerformed
    DBUtil objDBUtil = new DBUtil();
        Connection conn = objDBUtil.connect();
        //String maMonAn = txtMaMonAn.getText();
        try{
            int kq= JOptionPane.showConfirmDialog(this, "thong bao", "ban co muon xoa ko", JOptionPane.YES_NO_CANCEL_OPTION);
            if(kq==JOptionPane.YES_OPTION){
            AddNauAn.DeleteNauAn(maMonAn);
            JOptionPane.showMessageDialog(this, "xoa thanh cong", "thong bao", JOptionPane.CLOSED_OPTION);
            
            while (model.getRowCount()>0) {                
                model.removeRow(0);
            }LoadData();
                Clear();
            
            }
        }catch(Exception ex){
        ex.printStackTrace();
        }        
        
        
    }//GEN-LAST:event_cmd_xoaActionPerformed

    private void txtMaMonAnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMaMonAnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMaMonAnActionPerformed

    private void tab_monanMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tab_monanMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_tab_monanMouseEntered

    private void tab_monanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tab_monanMouseClicked
        // TODO add your handling code here:
        maMonAn = tab_monan.getValueAt(tab_monan.getSelectedRow(), 0).toString();
        txtMaMonAn.setText(tab_monan.getValueAt(tab_monan.getSelectedRow(), 0).toString());
        txt_tenmonan.setText(tab_monan.getValueAt(tab_monan.getSelectedRow(), 1).toString());
        ta_nguyenlieu.setText(tab_monan.getValueAt(tab_monan.getSelectedRow(), 2).toString());
        ta_quytrinh.setText(tab_monan.getValueAt(tab_monan.getSelectedRow(), 4).toString());
        ta_thietbi.setText(tab_monan.getValueAt(tab_monan.getSelectedRow(), 3).toString());
        
    }//GEN-LAST:event_tab_monanMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MonAn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                MonAn total = new MonAn();
                total.setLocationRelativeTo(total);
                total.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmd_dong;
    private javax.swing.JButton cmd_sua;
    private javax.swing.JButton cmd_them;
    private javax.swing.JButton cmd_xoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea ta_nguyenlieu;
    private javax.swing.JTextArea ta_quytrinh;
    private javax.swing.JTextArea ta_thietbi;
    private javax.swing.JTable tab_monan;
    private javax.swing.JTextField txtMaMonAn;
    private javax.swing.JTextField txt_tenmonan;
    // End of variables declaration//GEN-END:variables

    private String getURLConnection() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void LoadData() {
        
        DBUtil objDBUtil = new DBUtil();
        Connection conn = objDBUtil.connect();
        String SQL = "select * from MonAn";
    try {
        Statement stm = conn.createStatement();
        ResultSet rs = stm.executeQuery(SQL);
        model=(DefaultTableModel)tab_monan.getModel();
        while(rs.next()){
            Vector v = new Vector();
            v.addElement(rs.getString(1));
            v.addElement(rs.getString(2));
            v.addElement(rs.getString(3));
            v.addElement(rs.getString(4));
            v.addElement(rs.getString(5));
            model.addRow(v);
        }
        
        rs.close();
        stm.close();
    } catch (SQLException ex) {
        Logger.getLogger(MonAn.class.getName()).log(Level.SEVERE, null, ex);
    }
        //To change body of generated methods, choose Tools | Templates.
    }

    private void Clear() {
        txtMaMonAn.setText("");
        txt_tenmonan.setText("");
        ta_nguyenlieu.setText("");
        ta_quytrinh.setText("");
        ta_thietbi.setText("");
    }

    private static class model {

        private static void removeRow(int selectedRow) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public model() {
        }
    }

    private static class stm {

        private static ResultSet executeQuery(String select_from_MonAn) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public stm() {
        }
    }
}
